<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use app\modules\Cruise\models\Cruise;
use kartik\editable\Editable;

$this->title = Yii::t('app.cruise', 'Products');
$this->params['breadcrumbs'][] = $this->title;

$cruise = Cruise::find()->where(['status'=>1])->all(); 

//名单模板
$template = yii::$app->params['template'];
?>
<div class="product-index">

    <h2 class="page-header">
        <small><?= Html::encode($this->title) ?></small>
        <p class="pull-right">
            <?php if(Yii::$app->user->can("sale_check")){  ?>
                <?= Html::a('下载价格', "/product/product/product-price",['title'=>'下载价格', 'class'=>'btn btn-success', 'target'=>'_blank']) ?>
            <?php }?>
			<?php if(Yii::$app->user->can("order_assign")){  ?>
				<?= Html::a('护照审核', "/order/customer/passport-check",['title'=>'护照审核', 'class'=>'btn btn-info', 'target'=>'_blank']) ?>
			<?php } ?>
			<?php if(Yii::$app->user->can("sale_check")){  ?>
				<?= Html::a('优惠审批', "/order/order/sale-price",['title'=>'优惠审批', 'class'=>'btn btn-warning', 'target'=>'_blank']) ?>
				<?= Html::a('取消审批', "/order/room/refund",['title'=>'取消审批', 'class'=>'btn btn-danger', 'target'=>'_blank']) ?>

			<?php } ?>
            <!-- ?= Html::a(Yii::t('app.cruise', 'Create Product'), ['create'], ['class' => 'btn btn-success']) ? -->
            <button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal">创建团队</button>

        </p>
    </h2>
    <p><?php  echo $this->render('_search', ['model' => $searchModel]); ?></p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'layout'  =>  "{items}\n{pager}",
        'columns' => [
            //['class' => 'yii\grid\SerialColumn'],
            [
                'attribute' => 'name',
                'value' => function ($model) {
                       $url =  sprintf("/front/default/lines?id=%d", $model->id);
                       return Html::a($model->name, $url, ['title' =>$model->name, 'target'=>'_blank']);
                    },
                'format'=> 'raw',   
            ],
            //'code',
            'go_time:date',
            'return_time:date',
            [
                'attribute' => 'type',
                'value' => function ($model) {
                        return Yii::$app->params['product_type'][$model->type];
                    },
            ],
            [
                'attribute' => 'status',
                'filter' =>  Yii::$app->params['status'],
                'value' => function ($model) {
                        return Yii::$app->params['status'][$model->status];
                    },
            ],
            [
                'label'=>'订单',
                'format'=>'raw',
                'value' => function($model){
					$str = "数量 : " .$model->allOrderNum . '<br>'; 
					$str.= "金额 : " .$model->allOrderPrice . '<br>';
					$str.= "人数 : ".$model->allCustomerNum."<br>";
					$str.= "实收 : ". $model->financeSuccess;
					$str .= sprintf("<br>占位有效期: ");
    				$str .=	kartik\editable\Editable::widget([
    					    'name' => 'date_limit',
							'value' => $model->date_limit,
    					    'asPopover' => true,
    					    'header' => '占位期限 / 单位 天',
    					    'size'=>'md',
    					    'formOptions' => ['action'=>'/product/product/datelimit?id='.$model->id,'method'=>'get'],
    					    'options' => ['class'=>'form-control', 'placeholder'=>'设置占位期限','value'=> $model->date_limit],
    					    'pluginEvents'=> [
    					         "editableSuccess"=>"function(event, val, form, data) { 
    					             if(data){
    					                 $('#price').text(data.price); 
    					             }
    					         }",
    					    ],
    					]);
					$str .= " 天";

                    return "<small>" . $str . "</small>";
                }
            ],
            [
                'label'=>'库存',
                'format'=>'raw',
                'value' => function($model){
					$a = Html::a('详情','/order/order/stock?product_id='.$model->id, ['data-toggle'=>'modal', 'data-target'=>'.stock-modal']);
					if($model->stock == 1){
						$n = $model->stockAll-$model->surplusStock;
                    	return sprintf("总数 : %s <br> 剩余 : %s <br> %s", $model->stockAll, $n ,$a);
					}else{
						$n = $model->stockAll2-$model->surplusStock;
                    	return sprintf("总数 : %s <br> 剩余 : %s <br> %s", $model->stockAll2,$n, $a);
					}
                }
            ],
            [
                'label'=>'航线价格',
                'format'=>'raw',
                'value' => function($model){
                    $url  = sprintf("/product/product-room/create?product_id=%d", $model->id);
                    $url2 = sprintf("/product/product/trip?id=%d", $model->id);

                    $xdoc = sprintf("%s/product/product/word?id=%d&type=1",yii::$app->request->getHostInfo(),$model->id);
                    $xdoc_2 = sprintf("%s/product/product/word?id=%d&type=2",yii::$app->request->getHostInfo(),$model->id);
                    $_xdoc = urlencode($xdoc);
                    $_xdoc_2 = urlencode($xdoc_2);
                    $url3 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(同业价格)",$_xdoc,$model->cruise->name,$model->go_time);
                    $url_3 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(直客价格)",$_xdoc_2,$model->cruise->name,$model->go_time);

                    $url4 = sprintf("/order/order/index?product_id=%d", $model->id);
                    $url5 = sprintf("/product/product-extra/index?product_id=%d", $model->id);
                    $url6 = sprintf("/order/type/index?product_id=%d", $model->id);
                    $url10 = sprintf("/order/type/index?product_id=%d", $model->id);

                    $str  = Html::a('<i class="fa fa-cny"></i>',$url, ['title' =>'舱位价格','target'=>'_blank']).'&nbsp;&nbsp;'; 
					$str .= Yii::$app->user->can("order_assign") ? Html::a('<i class="fa fa-money"></i>',$url10,['title'=>'附加费配置','target'=>'_blank']).'&nbsp;&nbsp;' : '' ; 
                    $str .= Html::a('<i class="fa fa-cog"></i>',$url2,['title' =>'线路配置','target'=>'_blank']).'&nbsp;&nbsp;'; 
                    $str .= Html::a('<i class="fa fa-eye"></i>',$url4,['title' => '订单', 'target'=>'_blank']);

                    return $str;
                }
            ],

            [
                'label'=>'操作',
                'format'=>'raw',
                'value' => function($model){
                    $url  = sprintf("/product/product/copy?id=%d", $model->id);
                    $url2 = sprintf("/order/order/assign?product_id=%d", $model->id);
                    $url4 = sprintf("/order/room/list?product_id=%d", $model->id);
                    $url5 = sprintf("/order/order/commiss-list?product_id=%d", $model->id);

                    $str  = Html::a('<i class="fa fa-copy"></i>',$url,['title' =>'复制团队','target'=>'_blank']) .'&nbsp;&nbsp;'; 
					$str .= Yii::$app->user->can("order_assign") ? Html::a('<i class="fa fa-ship"></i>',$url4,['title' =>'分房号','target'=>'_blank']).'&nbsp;&nbsp;' : ''; 

					$str .= Yii::$app->user->can("order_assign") ? Html::a('<i class="fa fa-bus"></i>',$url2,['title' =>'订单分团','target'=>'_blank']).'<br>' : ''; 

					$str .= Yii::$app->user->can("order_assign") ? Html::a('<i class="fa fa-gg"></i>',$url5,['title' =>'后返列表','target'=>'_blank']).'<br>' : ''; 

					return $str;
                }
            ],
            [
                'label'=>'下载',
                'format'=>'raw',
                'value' => function($model){
                    $url3 = sprintf("/order/order/pack?product_id=%d", $model->id);
                    $url7 = sprintf("/order/order/card-pack?product_id=%d", $model->id);
                    $url6= sprintf("/order/word/sell-excel?product_id=%d", $model->id);
                    $url8= sprintf("/order/word/report-excel?product_id=%d", $model->id);
                    $xdoc = sprintf("%s/product/product/word?id=%d&type=1",yii::$app->request->getHostInfo(),$model->id);
                    $xdoc_2 = sprintf("%s/product/product/word?id=%d&type=2",yii::$app->request->getHostInfo(),$model->id);
                    $_xdoc = urlencode($xdoc);
                    $_xdoc_2 = urlencode($xdoc_2);

                    $xdoc_3 = sprintf("%s/product/product/neword?id=%d&type=1",yii::$app->request->getHostInfo(),$model->id);
                    $xdoc_4 = sprintf("%s/product/product/neword?id=%d&type=2",yii::$app->request->getHostInfo(),$model->id);
                    $_xdoc_3 = urlencode($xdoc_3);
                    $_xdoc_4 = urlencode($xdoc_4);

                    $url4 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(同业价格)",$_xdoc,$model->cruise->name,$model->go_time);
                    $url5 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(直客价格)",$_xdoc_2,$model->cruise->name,$model->go_time);

                    $url14 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(同业价格)",$_xdoc_3,$model->cruise->name,$model->go_time);
                    $url15 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(直客价格)",$_xdoc_4,$model->cruise->name,$model->go_time);

                    $str  = Yii::$app->user->can("is_admin") ? Html::a('<i class="fa fa-id-badge"></i>',$url3,['title' =>'打包护照资料','target'=>'_blank']) . '&nbsp;&nbsp;' : '';
                    $str  .= Yii::$app->user->can("is_admin") ? Html::a('<i class="fa fa-file-excel-o"></i>','javascript:;',['title' =>'生成名单',"data-product-id"=>$model->id,"class"=>"create_excel"]).'&nbsp;&nbsp;' : '';  

                    $str .= Html::a('<i class="fa fa-file-word-o"></i>',$url4,['title' => '生成WORD包含同业价格', 'target'=>'_blank']).'&nbsp;&nbsp;';
                    $str .= Html::a('<i class="fa fa-file-o"></i>',$url5,['title' => '生成WORD不包含同业价格', 'target'=>'_blank']).'&nbsp;&nbsp;';
                    $str .= Html::a('<i class="fa fa-file-text-o"></i>',$url6,['title' => '下载航期订单列表', 'target'=>'_blank']).'&nbsp;&nbsp;';
                    $str .= Yii::$app->user->can("order_assign") ? Html::a('<i class="fa fa-id-card-o"></i>',$url7,['title' =>'身份证或户口本资料下载','target'=>'_blank']) . '&nbsp;&nbsp;' : '';

                    $str .= Yii::$app->user->can("is_admin") ? Html::a('<i class="fa fa-plane"></i>',$url8,['title' => '边检报备表', 'target'=>'_blank']).'&nbsp;&nbsp;' : '';

                    $str .= Html::a('<i class="fa fa-file-picture-o"></i>',$url14,['title' => '生成最新WORD包含同业价格', 'target'=>'_blank']).'&nbsp;&nbsp;';
                    $str .= Html::a('<i class="fa fa-file-powerpoint-o"></i>',$url15,['title' => '生成最新WORD不包含同业价格', 'target'=>'_blank']).'&nbsp;&nbsp;';

                    return $str;
                }
            ],

            [
                'class' => 'yii\grid\ActionColumn',
                'template' =>"{view} {update}"
            ],
        ],
    ]); ?>
</div>


<!-- modal -->
<div class="modal fade" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">选择航线模板</h4>
      </div>
      <div class="modal-body">
            <div class="form-group row">
                <label class="control-label col-sm-2" for="cruise-supplier_id">选择游轮</label>
                <div class="col-md-7">
                    <select id="product-cruise_id" class="form-control">
                        <option value="">选择游轮</option>
                        <?php foreach($cruise as $val){  ?>
                            <option value="<?= $val->id ?>"><?= $val->name ?></option>
                        <?php } ?>
                    </select>
                </div>
            </div>
            
            <div id="template-list"><!-- 模板列表 -->
                
            </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default btn-primary" data-dismiss="modal">确定</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<div class="modal fade stock-modal"   role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:740px;">
      <div class="modal-content">
	 		<!-- 嵌入内容 --> 
	  </div>
  </div>
</div>

<!-- 选择游轮模板名单 -->
<div class="modal fade product-modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document">
      <div class="modal-content">
           
            <input type="hidden" name="product_id" id="product_id" value="">
            <ul class="list-group">
                <li class="list-group-item" style="background-color:#666;color:#FFF;">选择名单模板</li>
                <?php foreach($template as $k=>$v){ ?>
                <li class="list-group-item"><label><input type="radio" name="template_id" value="<?= $k?>"> <?= $v ?></label></li>
                <?php } ?>
                <li class="list-group-item"><button class="btn btn-primary" id="create">确定 </button></li>
            </ul>
            
      </div>
  </div>
</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">  
// 清除模态框缓存
$(".stock-modal").on("hidden.bs.modal", function() {
	$(this).removeData("bs.modal");
});
    // 选择模板
    $("#product-cruise_id").on("change", function() {
        var cruise_id = $(this).val();
         
        $.get("/product/template/get-by-cruise",{'cruise_id': cruise_id}, function(data){
            data =  jQuery.parseJSON(data);

            // 获取游轮对应的模板 
            if(data){
                $("#template-list").empty();
                $.each(data, function(k, model){
                    var html = "<div class=''>" + 
                               "<label class='radio-inline'>"+
                               "<input type='radio' name='template-check' class='template-check' value='"+ model.id +"' >"+ model.name + 
                               "</label>"+
                               "</div>";
                    $("#template-list").append(html);
                });
            }
        });
    });
    // 提交选择的模板
    $('#myModal').on('hidden.bs.modal', function (e) {
        var val = $('input[name="template-check"]:checked ').val();

        if(val !== undefined && val !== 'undefined'){
            // 传递模板编号生成新线路
            window.location.href = "/product/product/clone?template_id="+val;
        }
    })

    $(".product-modal").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
    });

    $(".create_excel").click(function(){
         $('.product-modal').modal('show');
         var product_id = $(this).attr("data-product-id");
         $("#product_id").val(product_id);
    });

    $("#create").click(function(){
        var product_id = $("#product_id").val();
        var template_id = $("input[name='template_id']:checked").val();
        if(!template_id){
            alert("未选择名单模板");
            return false;
        }
        var url = "/order/customer/create-excel?product_id="+product_id+"&template_id="+template_id;
        window.open(url);

    });
</script>
<?php app\components\JsBlock::end() ?>
